package com.sanbi.erp.api.web;

import com.sanbi.erp.api.vo.ResponseVo;
import com.sanbi.erp.core.dto.CategoryDto;
import com.sanbi.erp.core.system.ErrorEnum;
import com.sanbi.erp.db.config.BizException;
import com.sanbi.erp.db.domain.Category;
import com.sanbi.erp.db.service.CategoryService;
import com.sanbi.erp.db.util.CheckoutUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @introduce: 类目服务
 * @Date: 2022/10/17 17:06
 * @Author: XYP
 */
@Validated
@RestController
@RequestMapping("/erp/category")
public class ErpCategoryController {
    @Resource
    CategoryService categoryService;

    /**
     * 查询货品类目
     * 根据名称模糊匹配二级类目
     * @param name 类目中文名称
     * @return
     */
    @GetMapping("/getList")
    public ResponseVo<List<CategoryDto>> getList(@RequestParam(defaultValue = "") String name){
        if (CheckoutUtil.isEmptyString(name)) name = null;
        return ResponseVo.ok(this.categoryService.selectByLike(name));
    }

    /**
     * 新增类目
     * @param body
     * {name 类目名称,type 0:1级类目 1:2级类目,pid 父类目id}
     * @return
     */
    @PostMapping("/save")
    public ResponseVo<Integer> save(@RequestBody String body) {
        if (CheckoutUtil.isEmptyString(body)) {
            throw new BizException(ErrorEnum.ErrorCode.BadReqArgument);
        }
        return ResponseVo.ok(this.categoryService.insertSelectiveByType(body));
    }

    /**
     * 修改类目
     * @param body
     * {id 类目id,name 类目名称}
     * @return
     */
    @PostMapping("/alter")
    public ResponseVo<Integer> alter(@RequestBody String body) {
        if (CheckoutUtil.isEmptyString(body)) {
            throw new BizException(ErrorEnum.ErrorCode.BadReqArgument);
        }
        return ResponseVo.ok(this.categoryService.updateByPrimaryKeySelectiveTwo(body));
    }

    /**
     * 删除类目
     * @param id 类目id
     * @return
     */
    @GetMapping("/remove")
    public ResponseVo<Integer> remove(@RequestParam Integer id) {
        if (CheckoutUtil.isEmptyInteger(id)) {
            throw new BizException(ErrorEnum.ErrorCode.BadReqArgument);
        }
        return ResponseVo.ok(this.categoryService.removeByPrimaryKeySelectiveTwo(id));
    }

    /**
     * 获取所有二级类目
     * @return
     */
    @GetMapping("/getListTwo")
    public ResponseVo<List<Category>> getListTwo(){
        return ResponseVo.ok(this.categoryService.selectAllByTwo());
    }

}
